/*	
 *	jQuery fantasycolorsel 0.0.1
 *	Demo's and documentation:
 *	fantasycolorsel.davidsite.info 
 *	
 *	Copyright (c) 2011 http://weibo.com/phantasy66
 *	www.davidsite.info 
 *
 *	Dual licensed under the MIT and GPL licenses.
 *	http://en.wikipedia.org/wiki/MIT_License
 *	http://en.wikipedia.org/wiki/GNU_General_Public_License
 */
;(function($) {
	$.fn.colorSelect = function(option){
		if (this.length == 0) return log('No element selected.');
		if (this.length > 1) {
			$(this).colorSelect(option);
		}
		
		// 默认选项
		var o = $.extend({
			opts : undefined,
			afterSelect : undefined,
			hoverEffect:'fisheye',
			ishover:false,
			chdOptTitle:'Options',
			imgwidth:30,
			optsvisible:false,
			choosevisible:true
		}, option);
		
		var $optt= $(o.opts);
		var $this= this;
		var $tt  = $(this);
		var ishover=o.ishover;
		var hoverEffect=o.hoverEffect;
		var afterSelect=o.afterSelect;
		var imgwidth=o.imgwidth;
		var chdOptTitle=o.chdOptTitle;
		var optsvisible=o.optsvisible;
		var choosevisible=o.choosevisible;
		$this.bind_event=function(){
			
			$tt.bind('click',function(){
				if($optt.is(":visible")){
					//收起
					$this.hideOpts();
				}else{
					//展开
					$this.showOpts();
				}
			});
			
			$optt.find('li a').bind('click',function(){
				//消除Hover效果
				if(ishover&&$(this).hasClass('hovered')){
					var hoverEffectFuncs=$this.getHoverEffect(hoverEffect);
					hoverEffectFuncs.outFunction($(this));
					$(this).stop(false,true);
					$(this).removeClass('hovered');
				}
				var ul=$(this).parent().children('ul');
				if(ul.length==0)
				{//点击节点没有子节点
					$this.hideOpts();
					var p=$(this).parents('.colorSelchd');
					var c=undefined;
					if(p.length==0)
					{
						p=$(this);
						$tt.empty();
						$tt.append(p.parent().children(':first-child').clone());
					}else{
						p=$('.colorSelSelected').parent().children('a');
						c=$(this);
						$tt.empty();
						$tt.append(p.parent().children(':first-child').clone());
						$tt.append(c.parent().children(':first-child').clone());
					}
					if(o.afterSelect){
						o.afterSelect(p,c);
					}
					return;
				}
				//点击节点有子节点 
				if(!ul.hasClass('colorSelSelected'))
				{//点击不是已点击过的
					$('.colorSelSelected').removeClass('colorSelSelected');
					$('.colorSelchd').remove();
					ul.addClass('colorSelSelected');
					var offset = $(this).position();
					//计算出现li的位置
					var index=0;
					var as=$optt.find('>li > a');
					for(var i=0;i<as.length;i++)
					{
					    var of=$(as[i]).position();
					    if(of.top>offset.top)
					    {
					    	index=i-1;
					    	break;
					    }
					}
					if(index==0)
						index=as.length-1;
					//创建dom
					var e= $('<li class="colorSelchd" style="width: 100%; "><div class="colorSelchdopt" style="width: 100%;"><div class="colorSelchdtitle">'+chdOptTitle+'</div></div></li>');
					$(as[index]).parent().after(e);
					$('.colorSelchdopt').append(ul.clone(true).show());
					//计算箭头位置
				    var totalWidth = $(this).outerWidth();
					var leftoutter=parseInt($(this).parent().css("margin-left"), 10)+parseInt($(this).parent().css("padding-left"), 10);
					
					var left= offset.left+totalWidth/2-imgwidth/2-leftoutter;
					$('.colorSelchd').css('background-position',left+'px 0px');
					e.hide();
					//显示子菜单
					e.slideDown(300);
				}else{
					//点击当前元素
					if($('.colorSelchd').is(":visible"))
					{
						$('.colorSelchd').slideUp(300);
					}else{
						
						$('.colorSelchd').slideDown(300);
					}
				}
			});
			
			if(ishover){
				var hoverEffectFuncs=$this.getHoverEffect(hoverEffect);
				$optt.find('li a').hover(function(){
					if(!$(this).hasClass('hovered')){
						$(this).addClass('hovered');
						hoverEffectFuncs.inFunction($(this));
					}
				},function(){
					if($(this).hasClass('hovered')){
						$(this).removeClass('hovered');
						hoverEffectFuncs.outFunction($(this));
					}
				});
			}
		};
		
		$this.getHoverEffect=function(hoverEffect)
		{
			if(hoverEffect){
				if(typeof(hoverEffect)=='string')
				{//内置
					return $this.effect[hoverEffect];
				}else if(typeof(hoverEffect)=='object'){
					
					return hoverEffect;
				}
			}
			return {'inFunction':function(){},'outFunction':function(){}};
		};
		
		$this.initColor=function(){
			$optt.find('li a').each(function(a,i){
				if($(i).attr('color')){
					$(i).css('background',$(i).attr('color'));
				}
			});
		};
		
		$this.initDom=function(){
			$optt.find('li ul').hide();
			if(!optsvisible)
				$optt.hide();
			if(!choosevisible)
				$tt.hide();
				
		};
		
		$this.effect={
				'fisheye':{
					'inFunction':function(i)
					{
						i.stop(true,true).animate({'padding-left':'+=10px','padding-right':'+=10px','padding-top':'+=10px'},100);
					},
					'outFunction':function(i)
					{
						i.stop(true,true).animate({paddingLeft:'-=10px',paddingRight:'-=10px',paddingTop:'-=10px'},100);
					}
				}
		};
		
		$this.showOpts=function()
		{
			if(!$optt.is(':visible'))
			{
				$optt.slideDown(200);
			}
		};
		
		
		$this.hideOpts=function()
		{
			$optt.find('li a').stop(true,true);
			if($optt.is(':visible'))
			{
				$optt.slideUp(200);
			}
		};
		$this.initDom();
		$this.initColor();
		$this.bind_event();
	};
	
	
	
	function log(m) {
		if (typeof m == 'string') m = 'colorSel: ' + m;
		if (window.console && window.console.log) window.console.log(m);
		else try { console.log(m); } catch(err) { }
		return false;
	}
	$.fn.colorselect = function(o) {
		return this.colorSelect(o);
	};
})(jQuery);